﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;
using Process;

namespace QuanLyDiem
{
    public partial class frmMonHoc_ThayDoi : Form
    {
        private static frmMonHoc_ThayDoi frm = null;
        private MonHoc monhoc = new MonHoc();

        private frmMonHoc_ThayDoi()
        {
            InitializeComponent();
            TimKiemMa(true);
        }

        private void TimKiemMa(bool Ma)
        {
            txtMa.Enabled = Ma;
            txtTen.Enabled = !Ma;
            cmbBoMon.Enabled = !Ma;
        }

        public static frmMonHoc_ThayDoi Create()
        {
            return Create(false);
        }

        public static frmMonHoc_ThayDoi Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmMonHoc_ThayDoi();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            frm.RefreshData();
            return frm;
        }

        private void frmMonHoc_ThayDoi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void cmniChiTiet_Click(object sender, EventArgs e)
        {
            btnSua.PerformClick();
        }

        private void cmniXoa_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void txtMa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnTimKien.PerformClick();
            }
        }

        private void rdTen_CheckedChanged(object sender, EventArgs e)
        {
            TimKiemMa(!rdTen.Checked);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvMonHoc.SelectedRows.Count > 0)
            {
                MonHoc mh = new MonHoc();
                mh.ThongTin.Ma = dgvMonHoc.SelectedRows[0].Cells["cMa"].Value.ToString();
                mh.ThongTin.Ten = dgvMonHoc.SelectedRows[0].Cells["cTen"].Value.ToString();
                mh.ThongTin.SoTinChi = int.Parse(dgvMonHoc.SelectedRows[0].Cells["cSoTC"].Value.ToString());
                mh.ThongTin.HeSoDiemQT = dgvMonHoc.SelectedRows[0].Cells["cHSDiemQT"].Value.ToString();
                mh.ThongTin.BoMon = dgvMonHoc.SelectedRows[0].Cells["cBoMon"].Value.ToString();
                mh.ThongTin.GhiChu = dgvMonHoc.SelectedRows[0].Cells["cGhiChu"].Value.ToString();

                frmMonHoc_Sua frm = frmMonHoc_Sua.Create(mh);
                frm.Owner = this;
                frm.UpdateSuccess += new UpdateMonHoc(frm_UpdateSuccess);
                frm.ShowDialog();
            }
        }

        private void frm_UpdateSuccess(object sender, InfoUpdateMonHoc e)
        {
            DataTable dt = (DataTable)dgvMonHoc.DataSource;
            DataRow dr = dt.NewRow();
            dr[0] = e.MonHoc.ThongTin.Ma;
            dr[1] = e.MonHoc.ThongTin.Ten;
            dr[2] = e.MonHoc.ThongTin.SoTinChi;
            dr[3] = e.MonHoc.ThongTin.HeSoDiemQT.Replace('.', ',');
            dr[4] = e.MonHoc.ThongTin.HeSoDiemT.Replace('.', ',');
            dr[5] = e.MonHoc.ThongTin.BoMon;
            dr[6] = e.MonHoc.ThongTin.GhiChu;
            dt.Rows.InsertAt(dr, dgvMonHoc.SelectedRows[0].Index + 1);
            dt.Rows.RemoveAt(dgvMonHoc.SelectedRows[0].Index);
        }

        private void RefreshData()
        {
            dgvMonHoc.DataSource = monhoc.ListAll();
            List<string> temp = (new BoMon()).ListName();
            temp.Insert(0, "Chọn");
            cmbBoMon.DataSource = temp;
        }

        private void frmMonHoc_ThayDoi_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void dgvMonHoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnSua.PerformClick();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int count = dgvMonHoc.SelectedRows.Count;
            if (count > 0)
            {
                DialogResult dalrs = MessageBox.Show("Bạn có chắc chán muốn xóa " + count.ToString() + " môn học không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                count = 0;
                if (dalrs == DialogResult.OK)
                {
                    MonHoc monhoc = new MonHoc();
                    foreach (DataGridViewRow item in dgvMonHoc.SelectedRows)
                    {
                        monhoc.ThongTin.Ten = item.Cells["cTen"].Value.ToString();
                        int check = monhoc.Remove();
                        if (check > 0)
                        {
                            count++;
                            dgvMonHoc.Rows.Remove(item);
                        }
                    }
                    if (count > 0)
                    {
                        MessageBox.Show("Đã xóa thành công " + count.ToString() + " môn học", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn các môn học muốn xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtMa.Text = "";
            txtTen.Text = "";
            cmbBoMon.SelectedIndex = 0;
            RefreshData();
        }

        private void btnTimKien_Click(object sender, EventArgs e)
        {
            if (rdTen.Checked)
            {
                if (cmbBoMon.SelectedIndex != 0)
                {
                    TimKiemTenBoMon(txtTen.Text, cmbBoMon.Text);
                    return;
                }
                TimKiemTenBoMon(txtTen.Text, "");
            }
            else
            {
                int ma = 0;
                if (!string.IsNullOrEmpty(txtMa.Text))
                {
                    ma = int.Parse(txtMa.Text);
                }
                dgvMonHoc.DataSource = monhoc.List(ma);
            }
        }

        private void TimKiemTenBoMon(string Ten, string BoMon)
        {
            string ten = "";
            string bomon = "";
            if (!string.IsNullOrEmpty(Ten))
            {
                ten = Ten;
            }
            if (!string.IsNullOrEmpty(BoMon))
            {
                bomon = BoMon;
            }
            dgvMonHoc.DataSource = monhoc.List(ten, bomon);
        }

        private void cmbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBoMon.SelectedIndex != 0)
            {
                TimKiemTenBoMon(txtTen.Text, cmbBoMon.Text);
                return;
            }
            TimKiemTenBoMon(txtTen.Text, "");
        }

        private void txtMa_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMa.Text) && !xString.IsNumber(txtMa.Text, StyleNumber.INTEGER))
            {
                txtMa.Text = "";
                MessageBox.Show("Bạn chỉ được nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtMa.Focus();
            }
        }
    }
}
